//公共方法，格式化
export function formatTen(num) {
	return num > 9 ? (num + "") : ("0" + num);
}

//防抖（按钮点击，表单输入，窗口调整）
export function debounce(fn, delay = 500) {
	var timer = null;
	return function() {
		uni.showLoading({ // 显示加载中loading效果
			title: "提交中...",
			mask: true //开启蒙版遮罩
		});
		var _this = this;
		var args = arguments;
		if (timer) clearTimeout(timer);
		timer = setTimeout(function() {
			fn.apply(_this, args);
		}, delay);
	};
}

// inputChange:_debounce(function(){
//   console.log(this.inputVal)
// },1000),

//节流（实时搜索，页面滚动，鼠标移动）
export function throttle(fn, delay = 500) {
	var lastTime, timer, delay;
	return function() {
		var _this = this;
		var args = arguments;
		var nowTime = Date.now();
		if (lastTime && nowTime - lastTime < delay) {
			if (timer) clearTimeout(timer);
			timer = setTimeout(function() {
				lastTime = nowTime;
				fn.apply(_this, args);
			}, delay)
		} else {
			lastTime = nowTime;
			fn.apply(_this, args);
		}
	}
}




/**
 * 日期时间相关
 */

//获取当前时间戳
export function getTimeStamp() {
	let date = new Date();
	let timestamp = new Date().getTime();
	return timestamp
}
//获取当前时间（2023-07-10 16:48:42）
export function normalTime() {
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hour = date.getHours();
	let minute = date.getMinutes();
	let second = date.getSeconds();
	return year + "-" + formatTen(month) + "-" + formatTen(day) + " " + formatTen(hour) + ":" + formatTen(minute) +
		":" + formatTen(second);
}
//获取当前年，月，日，星期
export function TimeYYDAY() {
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hour = date.getHours();
	let minute = date.getMinutes();
	let second = date.getSeconds();

	let weekArray = new Array("日", "一", "二", "三", "四", "五", "六");
	let week = weekArray[new Date(date).getDay()];
	let data = {
		year: year,
		month: formatTen(month),
		day: formatTen(day),
		week: week
	}
	return data;
}
// 获取xxx天之后日期（2023-07-31）
export function afterDate(days) {
	var dd = new Date();
	dd.setDate(dd.getDate() + days); //获取AddDayCount天后的日期
	var y = dd.getFullYear();
	var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0
	var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
	return y + "-" + m + "-" + d;
}
// 时间戳转换成时间（2023-07-10 17:10:45）
export function timestampToTime(timestamp) {
	var date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-'
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
	var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
	var h = date.getHours() + ':'
	var m = date.getMinutes() + ':'
	var s = date.getSeconds()
	return Y + M + D + h + m + s
}
//获取年月日(不传即当日)
export function getDateYMD(timestamp) {
	let now;
	if (timestamp) {
		now = new Date(timestamp)
	} else {
		now = new Date()
	}
	let y = now.getFullYear(),
		m = now.getMonth() + 1,
		d = now.getDate(),
		h = now.getHours(), //获取当前小时数(0-23)
		f = now.getMinutes(),
		n = (Math.ceil((now.getMinutes()) / 10)) * 10; //获取当前分钟数(0-59)  取整数
	return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
}
// 去掉日期后面时分秒（2018-01-08 00:00:00 --> 2018-01-08）
export function interceptDate(date) {
	var newDate = date.substr(0, 10); //截取字符串（开始位置和长度）
	return newDate;
}

/**
 * 正则校验
 */
//邮箱
export function validateEmail(s) {
	return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
}

//会计金额格式(1,322.00)
export function toMoney(str) {
	return ((Number(str).toFixed(2) + "").replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,'))
}
//手机号码
export function validateMobile(s) {
	return /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/.test(s)
}
//校验身份证号
export function validateIDCard(idCard) {
  return /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}(\d|X|x)$/.test(idCard);
}


//隐藏手机号
export function hidePhone(mobile) {
	if (typeof mobile !== 'string') {
	  console.log('传入的手机号码必须是字符串类型');
	  return;
	}
	if (!validateMobile(mobile)) {
	  console.log('传入的手机号码不合法');
	  return;
	}
	return mobile.substring(0, 3) + '****' + mobile.substring(7);
}


//隐藏身份证号(注意只能传字符串，数字丢失精度)
export function hideIDCard(idCard) {
  if (typeof idCard !== 'string') {
    console.log('传入的身份证号码必须是字符串类型');
    return;
  }
  var isValid = validateIDCard(idCard);
  if (!isValid) {
    console.log('传入的身份证号码不合法');
    return;
  }
  var length = idCard.length;
  var hiddenPartLength = length - 6; // 要隐藏的位数长度
  var hiddenPart = '*'.repeat(hiddenPartLength); // 用星号替代要隐藏的部分
  var hiddenIDCard = idCard.slice(0, 3) + hiddenPart + idCard.slice(length - 3);
  return hiddenIDCard;
}

// 校验银行卡
 export function validateBankCardNumber(bankCardNumber) {
  var pattern = /^[0-9]{13,19}$/;
  var isValid = pattern.test(bankCardNumber);
  return isValid;
}

//隐藏银行卡
export function hideBankCardNumber(bankCardNumber) {
  if (typeof bankCardNumber !== 'string') {
    console.log('传入的银行卡号码必须是字符串类型');
    return;
  }
  var isValid = validateBankCardNumber(bankCardNumber);
  if (!isValid) {
    console.log('传入的银行卡号码不合法');
    return;
  }
  var length = bankCardNumber.length;
  var hiddenPartLength = length - 8; // 要隐藏的位数长度
  var hiddenPart = '*'.repeat(hiddenPartLength); // 用星号替代要隐藏的部分
  var hiddenBankCardNumber = bankCardNumber.slice(0, 4) + hiddenPart + bankCardNumber.slice(length - 4);
  return hiddenBankCardNumber;
}



//身份证号解析（年龄 性别  出生日期）
export function IdCard_Info(UUserCard) {
	let briday = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12,
		14); //出生日期
	let sex = ''; //性别
	if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
		sex = '男'
	} else {
		sex = '女'
	}
	let myDate = new Date();
	let month = myDate.getMonth() + 1;
	let day = myDate.getDate();
	let age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
	if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <=
		day) {
		age++;
	}
	let ob = {
		briday: briday,
		sex: sex,
		age: age
	}
	return ob;
}